package com.atguigu.bigdata.spark.core.rdd.operator.action;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Spark04_RDD_Operator_Action_JAVA {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext sc = new JavaSparkContext(conf);

        List<Tuple2<String, Integer>> list = Arrays.asList(new Tuple2<String,Integer>("a", 1),
                new Tuple2<String,Integer>("a",2), new Tuple2<String, Integer>("b", 1),
                new Tuple2<String, Integer>("b", 1));
        JavaPairRDD<String, Integer> rdd = sc.<String, Integer>parallelizePairs(list,2);
        // countbykey  基于key来统计出现次数
        Map<String, Long> res = rdd.countByKey();
        System.out.println(res);
        // countbyvalue  基于Tuple2<String, Integer> 来统计 一样的内容会聚合累加
        // {(b,1)=2, (a,2)=1, (a,1)=1}
        Map<Tuple2<String, Integer>, Long> res1 = rdd.countByValue();
        System.out.println(res1);
        sc.stop();

    }
}
